import Cookies from 'js-cookie'

// 语言Key
export const LANGUAGE_KEY = 'language'

// 获取语言
export function getLanguage() {
  const chooseLanguage = Cookies.get(LANGUAGE_KEY)
  if (chooseLanguage) return chooseLanguage

  // 如果没有选择语言，使用浏览器语言
  const language = (navigator.language || navigator.browserLanguage).toLowerCase()
  const locales = ['zh', 'en', 'th']

  for (const locale of locales) {
    if (language.indexOf(locale) > -1) {
      return locale
    }
  }
  return 'zh'
}

// 设置语言
export function setLanguage(language) {
  Cookies.set(LANGUAGE_KEY, language, { expires: 365 })
}

const TokenKey = 'Admin-Token'
export function getToken() {
  return Cookies.get(TokenKey)
}
export function setToken(token) {
  return Cookies.set(TokenKey, token)
}
export function removeToken() {
  return Cookies.remove(TokenKey)
}
